package com.bdqn.controller;

import com.bdqn.entity.DataModel;
import com.bdqn.entity.Goods;
import com.bdqn.entity.Parameters;
import com.bdqn.service.GoodsManageService;
import com.bdqn.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@CrossOrigin
public class GoodsManageController {
    @Autowired
    GoodsManageService service;

    @RequestMapping(value = "GoodsInfo")
    public DataModel<Goods> getGoodsInfo(Parameters parameters){
        parameters.setPage((parameters.getPage()-1)*parameters.getLimit());
        DataModel<Goods> dataModel=new DataModel<>();
        dataModel.setCode(0);
        dataModel.setMsg("");
        dataModel.setCount(service.getCount(parameters));
        dataModel.setData(service.getGoodsInfo(parameters));
            return dataModel;
    }
    @RequestMapping(value = "addGoods")
    public int addGoodsInfo(HttpServletRequest request,Goods goods){


        goods.setOperatorName(UserUtil.getUserName(request));
        return service.addGoodsInfo(goods);
    }

    @RequestMapping(value = "delGoodsInfo")
    public int delGoodsInfo(@RequestParam(value="arr[]") int[] ids){
//       String[] stringIds = ids.split(",");
//
//        int[] array = Arrays.asList(stringIds).stream().mapToInt(Integer::parseInt).toArray();

        return service.delGoodsInfo(ids);
    }
    @RequestMapping(value = "updateGoods")
    public int updateGoodsInfo(Goods goods){
        return service.updateGoodsInfo(goods);
    }
}
